package com.wfs.suanfa.nio.xiaoliu.nio1;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * 服务端
 */
public class ServerSocketChannelDemo {
    public static void main(String[] args) {
        try {
            ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
            serverSocketChannel.socket().bind(new InetSocketAddress(9999));
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            while (true){
                SocketChannel socketChannel = serverSocketChannel.accept();
                socketChannel.read(byteBuffer);
                System.out.println("收到客户端消息:"+new String(byteBuffer.array()));
                byteBuffer.flip();
                socketChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
